package com.jameschen.trunPage;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;

import com.anoah.trunpage.R;
import com.jameschen.trunPage.view.CurlView;

/**
 * 简单的活动对旋度测试
 */
public class CurlActivity extends Activity {

    private CurlView mCurlView;

    boolean drawText = true; //决定是否显示文本或图片

    private int[] mBitmapIds = {R.drawable.p0, R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4};

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int index = 1;
        if (getLastNonConfigurationInstance() != null) {
            index = (Integer) getLastNonConfigurationInstance();
        }
        setContentView(R.layout.main);
        mCurlView = (CurlView) findViewById(R.id.curl);

        mCurlView.setBitmapProvider(new BitmapProvider());
        mCurlView.setSizeChangedObserver(new SizeChangedObserver());
        mCurlView.setCurrentIndex(index);
        mCurlView.setBackgroundColor(0xFF202830);

        // This is something somewhat experimental. Before uncommenting next
        // line, 请参阅注释的方法 CurlView.
        mCurlView.setEnableTouchPressure(true);
    }

    @Override
    public void onPause() {
        super.onPause();
        mCurlView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mCurlView.onResume();
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        return mCurlView.getCurrentIndex();
    }

    /**
     * 图片提供.
     */
    private class BitmapProvider implements CurlView.BitmapProvider {
        @Override
        public Bitmap getBitmap(int width, int height, int index) {
            boolean isLeftPage;
            if (index % 2 == 0) {// 左边
                index = index / 2;
                isLeftPage = true;
            } else {
                index = (index / 2);
                isLeftPage = false;
            }

            if (drawText) {
                Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap);
                canvas.drawColor(Color.WHITE);

                TextPaint paint = new TextPaint();
                paint.setColor(Color.BLACK);
                paint.setStyle(Style.FILL);
                paint.setTextSize(48);
                paint.setTextAlign(Align.LEFT);
                paint.setAntiAlias(true);
                final String text = "人之初，性本善。性相近，习相远。\n" +
                        "苟不教，性乃迁。教之道，贵以专。\n" +
                        "昔孟母，择邻处。子不学，断机杼。\n" +
                        "窦燕山，有义方。教五子，名俱扬。\n" +
                        "养不教，父之过。教不严，师之惰。\n" +
                        "子不学，非所宜。幼不学，老何为。\n" +
                        "玉不琢，不成器。人不学，不知义。\n" +
                        "为人子，方少时。亲师友，习礼仪。\n" +
                        "香九龄，能温席。孝于亲，所当执。\n" +
                        "融四岁，能让梨。弟于长，宜先知。\n" +
                        "首孝悌，次见闻。知某数，识某文。\n" +
                        "一而十，十而百。百而千，千而万。\n" +
                        "三才者，天地人。三光者，日月星。\n" +
                        "三纲者，君臣义。父子亲，夫妇顺。\n" +
                        "曰春夏，曰秋冬。此四时，运不穷。\n" +
                        "曰南北，曰西东。此四方，应乎中。\n" +
                        "曰水火，木金土。此五行，本乎数。\n" +
                        "十干者，甲至癸。十二支，子至亥。\n" +
                        "曰黄道，日所躔。曰赤道，当中权。\n" +
                        "赤道下，温暖极。我中华，在东北。\n" +
                        "寒燠均，霜露改。右高原，左大海。\n" +
                        "曰江河，曰淮济。此四渎，水之纪。\n" +
                        "曰岱华，嵩恒衡。此五岳，山之名。\n" +
                        "古九州，今改制。称行省，三十五。\n" +
                        "曰士农，曰工商。此四民，国之良。\n" +
                        "曰仁义，礼智信。此五常，不容紊。\n" +
                        "地所生，有草木。此植物，遍水陆。\n" +
                        "有虫鱼，有鸟兽。此动物，能飞走。\n" +
                        "稻粱菽，麦黍稷。此六谷，人所食。\n" +
                        "马牛羊，鸡犬豕。此六畜，人所饲。\n" +
                        "曰喜怒，曰哀惧。爱恶欲，七情具。\n" +
                        "青赤黄，及黑白。此五色，目所识。\n" +
                        "酸苦甘，及辛咸。此五味，口所含。\n" +
                        "膻焦香，及腥朽。此五臭，鼻所嗅。\n" +
                        "匏土革，木石金。丝与竹，乃八音。\n" +
                        "曰平上，曰去入。此四声，宜调协。\n" +
                        "高曾祖，父而身。身而子，子而孙。\n" +
                        "自子孙，至玄曾。乃九族，人之伦。\n" +
                        "父子恩，夫妇从。兄则友，弟则恭。\n" +
                        "长幼序，友与朋。君则敬，臣则忠。\n" +
                        "此十义，人所同。当师叙，勿违背。\n" +
                        "斩齐衰，大小功。至缌麻，五服终。\n" +
                        "礼乐射，御书数。古六艺，今不具。\n" +
                        "惟书学，人共遵。既识字，讲说文。\n" +
                        "有古文，大小篆。隶草继，不可乱。\n" +
                        "若广学，惧其繁。但略说，能知原。";
                StaticLayout staticLayout;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    staticLayout = StaticLayout.Builder.obtain(text, 0, text.length(), paint, width)
                            .build();
                } else {
                    staticLayout = new StaticLayout(text, paint, width, Layout.Alignment.ALIGN_CENTER,
                            1.5f, 0.0f, true);
                }
                staticLayout.draw(canvas);
				canvas.drawText(text, 0, height, paint);

                return bitmap;
            } else {
                Bitmap b;
                BitmapFactory.Options opt = new BitmapFactory.Options();
                opt.inPreferredConfig = Config.RGB_565;
                // opt.inSampleSize=2;
                opt.inPurgeable = true;
                opt.inInputShareable = true;

                // width/=2;
                // height/=2;

                // 获取资源图片
                b = BitmapFactory.decodeResource(getResources(),
                        getResources().getIdentifier("p" + index, "drawable", getPackageName()),
                        opt);

                if (isLeftPage) {
                    b = Bitmap.createBitmap(b, 0, 0, width, height);
                } else {
                    b = Bitmap.createBitmap(b, width, 0, width, height);
                }
                // c.drawBitmap(b, 0, 0, p);
                return b;
            }
        }

        @Override
        public int getBitmapCount() {
            return mBitmapIds.length * 2;//一张两页
        }

        @Override
        public int[] getBitmapSize() {
            int[] picSize = new int[2];
            // just read first pic
            Drawable pic = getResources().getDrawable(mBitmapIds[0]);
            picSize[0] = pic.getIntrinsicWidth() / 2;//after will div 2
            picSize[1] = pic.getIntrinsicHeight();
            return picSize;
        }
    }

    /**
     * CurlView大小改变。
     */
    private class SizeChangedObserver implements CurlView.SizeChangedObserver {
        @Override
        public void onSizeChanged(int w, int h) {
            if (w > h) {
                mCurlView.setViewMode(CurlView.SHOW_TWO_PAGES);
                // mCurlView.setMargins(.1f, .05f, .1f, .05f);
            } else {
                mCurlView.setViewMode(CurlView.SHOW_ONE_PAGE);
                // mCurlView.setMargins(.1f, .1f, .1f, .1f);
            }
        }
    }
}